package cn.kivensoft.util;

import java.util.Objects;

/** 键值对泛型类
 * @author kiven
 *
 * @param <K>
 * @param <V>
 */
public class Pair <K, V> {
	private K first;
	private V second;
	
	public Pair() { }
	
	public Pair(K first, V second) {
		this.first = first;
		this.second = second;
	}
	
	public static <K, V> Pair<K, V> of(K first, V second) {
		return new Pair<>(first, second);
	}
	
	final public Pair<K, V> sets(K first, V second) {
		this.first = first;
		this.second = second;
		return this;
	}

	final public K getFirst() {
		return first;
	}
	
	final public void setFirst(K first) {
		this.first = first;
	}
	
	final public V getSecond() {
		return second;
	}
	
	final public void setSecond(V second) {
		this.second = second;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((first == null) ? 0 : first.hashCode());
		result = prime * result + ((second == null) ? 0 : second.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null || getClass() != obj.getClass()) return false;
		Pair<?, ?> other = (Pair<?, ?>) obj;
		return Objects.equals(first, other.first) && Objects.equals(second, other.second);
	}

	@Override
	public String toString() {
		return "Pair {first: " + first + ", second: " + second + "}";
	}

}
